package day_2022_7_to_8.day48;

/**
 * @author haomin
 * @date 2022/09/02 11:24
 **/
public class Day48_2_Offer36_TreeToDoublyList {
    class Solution {
        Node pre, head;
        public Node treeToDoublyList(Node root) {
            if(root == null) return null;
            inOrder(root);
            head.left = pre;
            pre.right = head;
            return head;
        }
        private void inOrder(Node root){
            if(root == null) return;
            inOrder(root.left);
            if(pre == null) head = root;
            else {
                pre.right = root;
                root.left = pre;
            }
            pre = root;
            inOrder(root.right);
        }
    }

    class Node {
        public int val;
        public Node left;
        public Node right;

        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val,Node _left,Node _right) {
            val = _val;
            left = _left;
            right = _right;
        }
    };
}